<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	//$couponset=cfc('jy_weishop')->plugin_setting('coupon');
	$coupontype=intval($_RQ['coupontype']);
	$params = array('uniacid'=>$_SESSION['uniacid']);
	if($coupontype !=10){
		if($coupontype){
			$params['coupontype']=$coupontype;
		}
		$coupon=pdo_getall('jy_weishop_coupon',$params);
		foreach($coupon as &$row){
				if ($row['t'] != -1){
					if ($row['last'] <= 0) 
					{
						$row['last'] = 0;
						$row['isdisa'] = '1';
					}
					else 
					{
						$totle = $row['t'];
						$row['lastratio'] = intval(($row['last'] / $totle) * 100);
					}
				}else{
					$row['last'] = 1;
					$row['lastratio'] = 100;
				}
		if ($row['coupontype'] == '2') {
					if (0 < $row['enough']) 
					{
						$title2 = '充值满￥' . (double) $row['enough'] . '元可用';
					}
					else 
					{
						$title2 = '无金额门槛';
					}
		}
		if ($row['coupontype'] == '1') {
					if (0 < $row['enough']) 
					{
						$title2 = '满' . (double) $row['enough'] . '元可用';
					}
					else 
					{
						$title2 = '无金额门槛';
					}
		}
		if ($row['backtype'] == 0) {
					$title3 = '￥' . (double) $row['deduct'];
					if ($row['enough'] == '0') 
					{
						$title5 = '消费任意金额立减' . (double) $row['deduct'].'元';
						$row['color'] = 'red ';
						$tagtitle = '代金券';
					}
					else 
					{
						$title5 = '消费满￥' . (double) $row['enough'] . '立减' . (double) $row['deduct'].'元';
						$row['color'] = 'blue';
						$tagtitle = '满减券';
					}
		}elseif ($row['backtype'] == 1) {
					$row['color'] = 'red ';
					$title3 = (double) $row['discount'] . ' 折 ';
					$tagtitle = '打折券';
					if ($row['enough'] == '0') 
					{
						$title5 = '消费任意金额' . '打' . (double) $row['discount'] . '折';
					}
					else 
					{
						$title5 = '消费满￥' . (double) $row['enough'] . '打' . (double) $row['discount'] . '折';
					}
		}elseif ($row['backtype'] == 2) {
					if ($row['coupontype'] == '0') 
					{
						$row['color'] = 'red ';
						$tagtitle = '购物返现券';
					}
					elseif ($row['coupontype'] == '2') 
					{
						$row['color'] = 'pink ';
						$tagtitle = '充值返现券';
					}elseif ($row['coupontype'] == '1') 
					{
						$row['color'] = 'red ';
						$tagtitle = '购物返现券';
					}
					if ($row['enough'] == '0') 
					{	
						$title5 = $row['coupontype'] == 1 ? '消费任意金额' : '充值任意金额';
					}else {
						$title5 = $row['coupontype'] == 1 ? '消费满￥' . (double) $row['enough'] : '充值满￥' . (double) $row['enough'];
					}
					if (!(empty($row['backmoney'])) && (0 < $row['backmoney'])) 
					{
						$title3 = '立返';
						$title5 = $title5 . '立返余额￥'.$row['backmoney'];
					}
					elseif (!(empty($row['backcredit'])) && (0 < $row['backcredit'])) 
					{
						$title3 = '立返';
						$title5 = $title5 . '立返积分'.$row['backcredit'];
					}
					elseif (!(empty($row['backredpack'])) && (0 < $row['backredpack'])) 
					{
						$title5 = $title5 . '立返红包￥'.$row['backcredit'];
					}
		}
		if ($row['tagtitle'] == '') {
					$row['tagtitle'] = $tagtitle;
		}
		if ($row['timelimittype'] == '0') {
					$title4 = '永久有效';
		}elseif ($row['timelimittype'] == '1') {
					$title4 = '即' . $row['gettypestr'] . '日内' . $row['timedays'] . '天有效';
		}else {
					$row['limittimes']=json_decode($row['limittimes'],true);
					$title4 = $row['limittimes']['tstart'].'~'.$row['limittimes']['tend'];
		}
				$row['title2'] = $title2;
				$row['title3'] = $title3;
				$row['title4'] = $title4;
				$row['title5'] = $title5;
				
				//发放时间：
				$row['sendtime']=json_decode($row['sendtime'],true);
				if($row['issendtime']){
					if($row['sendtime']['tstart'] > time()){
						$row['issend'] = 0;
					}elseif($row['sendtime']['tstart'] < time() && $row['sendtime']['tend'] > time()){
						$row['issend'] = 1;
					}else{
						$row['issend'] = 2;
					}
				}else{
					$row['issend'] = 1;
				}
		}
	}else{
		$coupon=pdo_getall('jy_weishop_wxcard',$params);
		foreach($coupon as &$row){
			if($row['card_type'] =='CASH'){
				$row['color'] = 'pink ';
				$tagtitle = '代金券';
				$title2 = '满'.$row['least_cost'].'可用';
				$title3 = '立减';
				$title5 = '消费满￥' . (double) $row['least_cost']. '立减' . (double) $row['reduce_cost'].'元';
			}else{
				$row['color'] = 'pink ';
				$tagtitle = '折扣券';
				$title2 = '满'.$row['least_cost'].'可用';
				$title3 = '立减';
				$title5 = '消费满￥' . (double) $row['least_cost']. '打' . (double) $row['discount'].'折';
			}
			if ($row['datetype'] == '0') {
					$title4 = '即' . $row['fixed_term'] . '天有效';
			}else{
					$row['timestamp']=json_decode($row['timestamp'],true);
					$title4 = $row['timestamp']['tstart'].'~'.$row['timestamp']['tend'];
			}
			
			$row['title2'] = $title2;
			$row['title3'] = $title3;
			$row['title4'] = $title4;
			$row['title5'] = $title5;
		}
		//用户领取卡券
		$res=cfc('account')->request('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={access_token}&type=wx_card');
		$api_ticket = $res['ticket'];
		$params=array(
			'api_ticket'=>$api_ticket,
			'timestamp'=>time(),
			'card_id'=>$row['card_id'],
		);
		$signature = sort($params);
		$cardExt = array('openid'=>$_SESSION['openid'],'timestamp'=>time(),'outer_str'=>'jy_weishop','signature'=>$signature);
		$row['cardExt'] = json_decode($cardExt,true);
	}
	$return['coupontype']=$coupontype;
	$return['couponset']=$couponset;
	$return['list']=$coupon;
	$return['ntime']=time();
	set_html('title',$setting['title']?'优惠券-'.$setting['title']:'优惠券-智慧电商客');
	exi($return);
}
if($op=='tx'){
	$post=get_postdata();
	$id = intval($post);
	pdo_insert('jy_weishop_sendmsg',array('mid'=>$_SESSION['uid'],'couponid' => $id,'time'=>time(),'issend'=>0,'msg'=>'优惠券领券提醒！'));
	exi('设置成功');
}
?>